<?php
/**
 * Created by 链行天下.
 * User: tangjianing(990921093@qq.com)
 * Date: 2019/2/1 10:40
 */

namespace App\Tools;

class VerifyWalletAddress
{
    const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';

    public static function verify($walletAddress, $prefix)
    {
        $prefixLen = strlen($prefix);
        //校验前缀
        if (!self::checkPrefix($walletAddress)) {
            return false;
        };
        //切除前缀
        $newWalletAddress = substr($walletAddress, $prefixLen);
        //base58解码
        $base58_decode =  self::base58_decode($newWalletAddress);
        //去掉最后的4个字节
        $string = substr($base58_decode , 0, -4);
        //取出最后4个字节
        $end_string = substr($base58_decode , -4);
        //ripemd160 hash
        $hash = hash('ripemd160', $string, true);
        //ripemd160之后取出前4位字节
        $hash_string = substr($hash , 0,4);
        return $end_string != $hash_string ? false : true;
    }

    protected static function checkPrefix($walletAddress)
    {
        return strpos($walletAddress, 'bcb') !== 0 ? false : true;
    }


    protected static function base58_encode($string)
    {
        $alphabet = self::ALPHABET;
        $base = strlen($alphabet);
        if (is_string($string) === false) {
            return false;
        }
        if (strlen($string) === 0) {
            return '';
        }
        $bytes = array_values(unpack('C*', $string));
        $decimal = $bytes[0];
        for ($i = 1, $l = count($bytes); $i < $l; $i++) {
            $decimal = bcmul($decimal, 256);
            $decimal = bcadd($decimal, $bytes[$i]);
        }
        $output = '';
        while ($decimal >= $base) {
            $div = bcdiv($decimal, $base, 0);
            $mod = bcmod($decimal, $base);
            $output .= $alphabet[$mod];
            $decimal = $div;
        }
        if ($decimal > 0) {
            $output .= $alphabet[$decimal];
        }
        $output = strrev($output);
        foreach ($bytes as $byte) {
            if ($byte === 0) {
                $output = $alphabet[0] . $output;
                continue;
            }
            break;
        }
        return (string) $output;
    }
    protected static function base58_decode($base58)
    {

        if (is_string($base58) === false) {
            return false;
        }
        if (strlen($base58) === 0) {
            return '';
        }
        $indexes = array_flip(str_split(self::ALPHABET));
        $chars = str_split($base58);
        $base = strlen(self::ALPHABET);
        foreach ($chars as $char) {
            if (isset($indexes[$char]) === false) {
                return false;
            }
        }
        $decimal = $indexes[$chars[0]];
        for ($i = 1, $l = count($chars); $i < $l; $i++) {
            $decimal = bcmul($decimal, $base);
            $decimal = bcadd($decimal, $indexes[$chars[$i]]);
        }
        $output = '';
        while ($decimal > 0) {
            $byte = bcmod($decimal, 256);
            $output = pack('C', $byte) . $output;
            $decimal = bcdiv($decimal, 256, 0);
        }
        foreach ($chars as $char) {
            if ($indexes[$char] === 0) {
                $output = "\x00" . $output;
                continue;
            }
            break;
        }
        return $output;
    }

}
